package com.neusoft.neumooc.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.minidev.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.neusoft.core.common.BaseController;
import com.neusoft.core.common.conf.Params;
import com.neusoft.core.common.exception.ServiceException;
import com.neusoft.core.util.MD5Util;
import com.neusoft.core.util.jwt.Jwt;
import com.neusoft.neumooc.service.UserService;


/** 
 * @ClassName: LoginController 
 * @Description: TODO 登录控制类
 * @author zls
 * @date 2016年11月3日
 */
@Controller
@RequestMapping(value="/login")
public class LoginController extends BaseController {
	
	/** 
	 * @Fields userService : TODO 登录业务逻辑处理类
	 */ 
	@Autowired
	UserService userService;
	
	
	/** 
	 * @Title: check 
	 * @Description: TODO 登录验证密码
	 * @param request
	 * @param response
	 * @param userName 用户名
	 * @param password 密码
	 * @return Map
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/check")
	public Map<String, Object> check(HttpServletRequest request, HttpServletResponse response, String userName, String password) throws Exception{
		JSONObject resultJSON = new JSONObject();
		String passwordMD5 = MD5Util.gen(password.getBytes());
		try {
			Map<String,Object> map = userService.getUserByName(userName);
			if(map != null && map.get("password").equals(passwordMD5)){
				Map<String, Object> payload = new HashMap<String, Object>();
				Date date = new Date();
				payload.put("uid", userName);// 用户ID
				payload.put("time", date.getTime());// 生成时间
				payload.put("ext", date.getTime() + 1000 * 60 * 60);// 过期时间1小时
				String token = Jwt.createToken(payload);
				resultJSON.put(Params.RET_CODE, Params.RET_CODE_SUCCESS);
				resultJSON.put("msg", "登陆成功");
				resultJSON.put("token", token);
			} else {
				resultJSON.put("success", false);
				resultJSON.put("msg", "用户名密码不对");
			}
		} catch (ServiceException e) {
			log.error("登录验证密码有异常信息抛出" + e.getMessage(),e.getCause());
			
		}
		return resultJSON;
	}
	
	
	/** 
	 * @Title: login 
	 * @Description: TODO 进入登录页面
	 * @param request
	 * @param response
	 * @return String
	 * @throws Exception
	 */
	@RequestMapping(value = "/login")
	public String login(HttpServletRequest request, HttpServletResponse response) throws Exception {
		return "login";
	}
	
	
	/** 
	 * @Title: logout 
	 * @Description: TODO 登出
	 * @param request
	 * @param response
	 * @return String
	 * @throws Exception
	 */
	@RequestMapping(value = "/logout")
	public String logout(HttpServletRequest request, HttpServletResponse response) throws Exception {
		HttpSession session = request.getSession();
		session.setAttribute(Params.SESSION_ATTR_KEY_USER, null);
		return "login";
	}
}
